/**
 * Copyright (c) 2011 Basil Shikin, VintageRadio Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.vintagephone.model.hardware;

import org.vintagephone.model.RadioControlListener;
import org.vintagephone.model.hardware.ioio.IOIORadioHardwareThread;


/**
 * This class contains support for radio hardware interaction
 * @author Basil Shikin
 *
 */
public class RadioHardwareControl
    implements RadioHardwareListener
{
    private final IOIORadioHardwareThread m_hardwareThread;
    
	private RadioControlListener m_listener;
    
	private int m_lastRotPosition;

	public RadioHardwareControl()
	{
	    m_hardwareThread = new IOIORadioHardwareThread();
	}
	
	public void initialize()
	{
	    m_hardwareThread.setListener( this );
	    m_hardwareThread.start();
	}
	
	public void setListener( RadioControlListener listener )
	{
		m_listener = listener;
	}

    @Override
    public void onStateChanged(boolean isOnNow)
    {
        if ( m_listener != null ) m_listener.offStateChaneged( !isOnNow );
    }

    @Override
    public void volumeChanged(float newVolume)
    {
        if ( m_listener != null ) m_listener.volumeChanged( Math.round(  100F*newVolume ) );
    }

    @Override
    public void buttonPressed(int buttonNo)
    {
        if (m_listener != null ) m_listener.folderChanged( buttonNo );
    }

    @Override
    public void songSelectorChanged(int rotPosition)
    {
        if ( rotPosition != m_lastRotPosition )
        {
            if ( m_listener != null ) m_listener.songChanged( rotPosition > m_lastRotPosition );
        }
        m_lastRotPosition = rotPosition;
    }
}
